package com.sfzy.controller;

import com.github.pagehelper.PageInfo;
import com.sfzy.entity.Course;
import com.sfzy.service.CourseService;
import com.sfzy.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


//service调用mapper，controller调用service
@RestController
@RequestMapping("course")
public class CourseController {
    @Autowired
    private CourseService courseService;

//增
    @PostMapping
    public R add(@RequestBody Course course) {
        int i = courseService.addCourse(course);
        if (i!=0){
            return R.ok("操作成功");
        }
        return R.fail("操作失败");
    }

//改
    @PutMapping
    public R update(@RequestBody Course course) {
        int i = courseService.updateCourse(course);
        if (i!=0){
            return R.ok("操作成功");
        }
        return R.fail("操作失败");
    }
//删
    @DeleteMapping("{id}")
    public R delete(@PathVariable("id") int id){
        int i = courseService.deleteCourse(id);
        if (i!=0){
            return R.ok("操作成功");
        }
        return R.fail("操作失败");
    }

//查id
    @GetMapping("{id}")
    public R get(@PathVariable("id") int id){
        Course course = courseService.getCourseById3(id);
        return R.ok(course);
    }
//分页查
    @GetMapping("page3")
    public R getByPage(Course course,
                       @RequestParam(required = false,defaultValue = "1") int pageNum,
                       @RequestParam(required = false,defaultValue = "5") int pageSize){
        PageInfo<Course> allCourseByPage = courseService.getAllCourseByPage3(course, pageNum, pageSize);
        return R.ok(allCourseByPage);
    }
}
